home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_03 / allison / machine.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-03  |  2.4 KB  |  99 lines

  1. LISTING 3 - Computes Machine Floating-point Parameters
  2.  
  3. #include <stdio.h>
  4. #include <math.h>
  5. #include <float.h>
  6.  
  7. main()
  8. {
  9.     int beta, p;
  10.     double a, b, eps, epsp1, sigma, nums;
  11.  
  12.     /* Discover radix */
  13.     a = 1.0;
  14.     do
  15.     {
  16.         a = 2.0 * a;
  17.         b = a + 1.0;
  18.     } while ((b - a) == 1.0);
  19.  
  20.     b = 1.0;
  21.     do
  22.         b = 2.0 * b;
  23.     while ((a + b) == a);
  24.     beta = (int) ((a + b) - a);
  25.     printf("radix:\n");
  26.     printf("\talgorithm: %d\n",beta);
  27.     printf("\tprovided: %d\n",FLT_RADIX);
  28.  
  29.     /* Compute precision in bits */
  30.     p = 0;
  31.     a = 1.0;
  32.     do
  33.     {
  34.         ++p;
  35.         a *= (double) beta;
  36.         b = a + 1.0;
  37.     } while ((b - a) == 1.0);
  38.     printf("precision:\n");
  39.     printf("\talgorithm: %d\n",p);
  40.     printf("\tprovided: %d\n",DBL_MANT_DIG);
  41.  
  42.     /* Compute machine epsilon */
  43.     eps = 1.0;
  44.     do
  45.     {
  46.         eps = 0.5 * eps;
  47.         epsp1 = eps + 1.0;
  48.     } while (epsp1 > 1.0);
  49.     epsp1 = 2.0 * eps + 1.0;
  50.     eps = epsp1 - 1.0;
  51.     printf("machine epsilon:\n");
  52.     printf("\talgorithm: %g\n",eps);
  53.     printf("\tformula: %g\n",pow(FLT_RADIX,1.0-DBL_MANT_DIG));
  54.     printf("\tprovided: %g\n",DBL_EPSILON);
  55.  
  56.     /* Compute smallest normalized magnitude */
  57.     printf("smallest non-zero magnitude:\n");
  58.     printf("\tformula: %g\n",pow(FLT_RADIX,DBL_MIN_EXP-1));
  59.     printf("\tprovided: %g\n",DBL_MIN);
  60.  
  61.     /* Compute larget normalized magnitude */
  62.     printf("largest non-zero magnitude:\n");
  63.     printf("\tformula: %g\n",
  64.            pow(FLT_RADIX,DBL_MAX_EXP-1) * FLT_RADIX *
  65.            (1.0 - pow(FLT_RADIX,-DBL_MANT_DIG)));
  66.     printf("\tprovided: %g\n",DBL_MAX);
  67.  
  68.     printf("smallest exponent: %d\n",DBL_MIN_EXP);
  69.     printf("largest exponent: %d\n",DBL_MAX_EXP);
  70.  
  71.     nums = 2 * (FLT_RADIX - 1)
  72.          * pow(FLT_RADIX,DBL_MANT_DIG-1)
  73.          * (DBL_MAX_EXP - DBL_MIN_EXP + 1);
  74.     printf("This system has %g numbers\n",nums);
  75.     return 0;
  76. }
  77.  
  78. /* Output (Borland C++):
  79. radix:
  80.      algorithm: 2
  81.      provided: 2
  82. precision:
  83.      algorithm: 53
  84.      provided: 53
  85. machine epsilon:
  86.      algorithm: 2.22045e-16
  87.      formula: 2.22045e-16
  88.      provided: 2.22045e-16
  89. smallest non-zero magnitude:
  90.      formula: 2.22507e-308
  91.      provided: 2.22507e-308
  92. largest non-zero magnitude:
  93.      formula: 1.79769e+308
  94.      provided: 1.79769e+308
  95. smallest exponent: -1021
  96. largest exponent: 1024
  97. This system has 1.84287e+19 numbers
  98. */
  99.